package com.neoden.notes;

import java.text.DateFormat;
import java.util.Date;

import com.neoden.notes.data.NotesProvider;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;


public class NotesListCursorAdapter extends CursorAdapter {

	public NotesListCursorAdapter(Context context, Cursor c) {
		super(context, c, false); // using autoRequery is discouraged
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView titleTextView = (TextView)view.findViewById(R.id.list_item_title);
		titleTextView.setText(cursor.getString(
				cursor.getColumnIndex(NotesProvider.Notes.COLUMN_TITLE)));
		
		TextView modifiedTextView = (TextView)view.findViewById(R.id.list_item_modified);

		Date modifiedDate = new Date(cursor.getLong(
				cursor.getColumnIndex(NotesProvider.Notes.COLUMN_MODIFIED_TS)));
		
		Date tenHoursAgo = new Date(System.currentTimeMillis() - (3600000L * 10)); 
		
		DateFormat df;
		if (tenHoursAgo.before(modifiedDate)) {
			df = android.text.format.DateFormat.getTimeFormat(context);
		}
		else {
			df = android.text.format.DateFormat.getDateFormat(context);
		}
		
		modifiedTextView.setText(df.format(modifiedDate));
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.list_item_note, parent, false);
		bindView(view, context, cursor);
		return view;
	}

}
